import formatDate from '../../../utils/formatdate'

Page({
  data: {
    id: undefined,
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseInfo: {},
    repairInfo: {},
    mobile: '',
    description: '',
    appointment: ''
  },
  onLoad(option) {
    this.getRomm()
    this.getRepair()
    let { id } = option
    if (!!id) {
      wx.setNavigationBarTitle({
        title: "修改保修信息"
      })
      this.setData({
        id
      })
      this.getInfo(id)
    }
  },
  async getRomm() {
    let res = await wx.http.get('/house')
    this.setData({
      houseList: res
    })
  },
  async getRepair() {
    let res = await wx.http.get('/repairItem')
    this.setData({
      repairItem: res
    })
  },
  async getInfo(id) {
    let res = await wx.http.get('/repair/' + id)
    this.setData({
      houseInfo: {
        name: res.houseInfo,
        id: res.houseId
      },
      repairInfo: {
        id: res.repairItemId,
        name: res.repairItemName
      },
      ...res
    })
  },
  selectHouse(event) {
    this.setData({
      houseInfo: event.detail
    })
  },
  selectRepair(event) {
    this.setData({
      repairInfo: event.detail
    })
  },
  selectDate(event) {
    this.setData({
      appointment: formatDate(new Date(event.detail), 'yyyy-MM-dd'),
      dateLayerVisible: false
    })
  },
  beforeRead(event) {
    const { file, callback } = event.detail;
    console.log(file)
    if (!/.jpg|.png/.test(file.url)) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
      return callback(false)
    }
    if (file.size > 1024 * 1024) {
      wx.showToast({
        title: '图片不能超过1MB',
        icon: 'none'
      })
      return callback(false)
    }
    callback(true);
  },
  async afterRead(event) {
    const { file } = event.detail;

    let res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: file.url
    })
    let arr = [...this.data.attachment]

    arr.push(res)

    this.setData({
      attachment: arr
    })
  },
  delImg(event) {
    this.setData({
      attachment: this.data.attachment.filter((item, index) => {
        return index != event.detail.index
      })
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goList() {

    const toast = (title) => {
      wx.showToast({
        title,
        icon: 'none'
      })
    }
    let { attachment, houseInfo, repairInfo, mobile, description, appointment } = this.data
    if (!houseInfo.hasOwnProperty('name') || houseInfo.name == "") {
      return toast("房屋信息不能为空")
    }
    if (!repairInfo.hasOwnProperty('name') || repairInfo.name == "") {
      return toast("维修项目不能为空")
    }
    if (mobile == "") {
      return toast("手机号码不能为空")
    }
    if (appointment == "") {
      return toast("预约日期不能为空")
    }
    if (description == "") {
      return toast("问题描述不能为空")
    }
    if (attachment.length == 0) {
      return toast("问题附件不能为空")
    }

    let paramData = {
      houseId: houseInfo.id,
      repairItemId: repairInfo.id,
      mobile,
      appointment,
      description,
      attachment,
      id: this.data.id
    }

    await wx.http.post('/repair', paramData)

    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
